<?php
// 编译项目建立结构
function bulidApp ()
{
    // 没有创建项目目录的话自动创建
    if (! is_dir(APP_PATH))
        MakeDir(APP_PATH, 0777);
    if (is_writeable(APP_PATH)) {
        $dirs = array(
                APP_COMMON,
                APP_CONFIG,
                APP_PLUS,
                APP_MODULE,
                APP_CONTROL,
                APP_VIEW,
                APP_LOG,
                APP_CACHE,
                APP_CACHE . DS . "session_tmp",
                APP_LANG
        );
        foreach ($dirs as $dir) {
            if (! is_dir($dir))
                MakeDir($dir, 0777);
        }
        // 目录安全写入
        defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', false);
        if (BUILD_DIR_SECURE) {
            addSafeFile($dirs);
        }
        // 写入配置文件
        if (! is_file(APP_CONFIG . DS . 'config.php'))
            file_put_contents(APP_CONFIG . DS . 'config.php', "<?php\nreturn array(\n\t//'配置项'=>'配置值'\n);\n?>");
            // 写入测试Action
        if (! is_file(APP_CONTROL . DS . 'WelcomeController.php'))
            buildFirstAction();
    } else {
        header('Content-Type:text/html; charset=utf-8');
        exit('项目目录不可写，目录无法自动生成！<BR>请修改目录可写权限.');
    }
}
// 创建测试Action
function buildFirstAction ()
{
    $content = file_get_contents(HEX_PATH . DS . 'Template' . DS . 'default_index.php');
    file_put_contents(APP_CONTROL . DS . 'WelcomeController.php', $content);
}
// 写入文件
function addSafeFile ($dirs)
{
    defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', DS . 'index.html');
    defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', 'dir');
    // 自动写入目录安全文件
    $content = DIR_SECURE_CONTENT;
    $a = explode(',', DIR_SECURE_FILENAME);
    foreach ($a as $filename) {
        foreach ($dirs as $dir) {
            file_put_contents($dir . $filename, $content);
        }
    }
}
/**
 * autoload
 *
 *
 * @param string $class            
 * @throws SDException
 */
function hexAutoload ($class)
{
    $class = str_replace("\\", DS, $class);
    $classPath = HEX_PATH . DS . $class . '.php';
    if (is_file($classPath)) {
        include_once $classPath;
    }
}
spl_autoload_register("hexAutoload"); // 注册自动载入函数，使得类的载入规则化
/*
 * 获取参数解析
 */
function getParam ()
{
    $url_mode = iniGet('url_mode');
    $depr = iniGet('url_pathinfo_depr');
    $var = array();
    switch ($url_mode) {
        case 1:
            $control = !empty($_GET[MODEL_NAME]) ? trim($_GET[MODEL_NAME]):"";
            $action = !empty($_GET[ACTION_NAME]) ? trim($_GET[ACTION_NAME]):"";
            break;
        case 2:
            if(isset($_SERVER['PATH_INFO'])){
				$paths = explode("/",trim($_SERVER['PATH_INFO'],"/"));
				$control = array_shift($paths);
				$action = array_shift($paths);
				if(iniGet('var_url_params')) {
				    // 直接通过$_GET['_URL_'][1] $_GET['_URL_'][2] 获取URL参数 方便不用路由时参数获取
				    $_GET[iniGet('var_url_params')]   =  $paths;
				}
				//解析pathinfo格式 成数组
				$res = preg_replace('@(\w+)'.$depr.'([^'.$depr.'\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode($depr,$paths));
				$_GET   =  array_merge($var,$_GET);
			}
            break;
    }
    $_GET[MODEL_NAME] = $control = !empty($control) ? $control : DEFAULT_MODEL;
    $_GET[ACTION_NAME] = $action = !empty($action)  ? $action  : DEFAUTL_ACTION;
    return array($control=>$action);
}
function halt($msg){
    exit($msg);
}
